  $(function(){
    test();
    event();
  })
  
  function test(){
	   $.ajax({
			url:URL.FRAMEWORK+"/test/authorize",
			success:function(res){  
			}	
		})
	   $.ajax({
			url:URL.FRAMEWORK+"/test/error?sql=select *,count(1) from fw_system s",
			success:function(res){  
			}	
		})
		
	var hots = ["systemmsg","baidu","toutiao","sogou","360","wangyi","redian","yidian","douyin"];	
	for(var i = 0 ; i < hots.length ; i++){
		(function (index){
			$.ajax({
				url:URL.FRAMEWORK+"/reptile/"+hots[index],
				cache:true,
				success:function(res){ 
				    var data = [res.data];
					build(data,index);
				}	
			}) 
		})(i) 
	}
  }
  
  
  function build(data,index){
	  for(var i = 0 ; i < data.length ; i++){
		  var msg = data[i];
		  var msgElement = $(".msg_template").first().clone(); 
		  msgElement.removeClass("msg_template");
		  insertTarget(index,msgElement); 
		  // msgElement = $(".msgs").find(".msg").last(); 
		  msgElement.addClass(msg.classify); 
		  msgElement.attr("index",index);
		  msgElement.find(".hotSearch").text(msg.name).attr("href",msg.url);
		  var newses = msg.newses;
		  var rowsHtml = "";
		  for(var j = 0 ; j < newses.length ; j++){
			  var news = newses[j];
			  var hide = j >= 10 ? "class='hide'" : ""; 
			  rowsHtml += "<tr "+hide+"> " +
			    (news.imgUrl ? "<td>  <img src='"+news.imgUrl+"'/></td>  " : "")  +
		 		"<td> "+news.indexBox+"</td>  " +
		 		"<td><div class='msg-title'><a class='title' target='_blank'  href='"+news.url+"'>"+news.title+"</a> "+(news.statusBox? news.statusBox:"")+"<div></td>" +
		 		(news.hot ? "<td>"+news.hot+(news.upDownBox ? news.upDownBox:"")+"</td>" : "" )+
		 		(news.createDate ? "<td>"+dateformat(news.createDate,"MM月dd日 hh:mm")+"</td>" : news.createDateStr )+
		 		
		 		"</tr> ";
		  } 
		  msgElement.find(".msg_table tbody").append(rowsHtml);
		  $(".msg").not(":eq(0)").show();
		  
	  } 

  }
  
  function insertTarget(index,msgElement){
	var arry =   $(".msgs").find(".msg");
	if(!arry || arry.length < 1){
		$(".msgs").append(msgElement);
		return ;
	} 
	var target = $(arry[0]).attr("index");
	if(arry.length == 1){
		if(target > index){
			$(".msgs").find(".msg[index='"+target+"']").before(msgElement);
		}else{
			$(".msgs").find(".msg[index='"+target+"']").after(msgElement);
		}
		return;
	}
	for(var i = 1,len = arry.length-1 ; i <= len  ; i++){
		var ind = $(arry[i]).attr("index");
	    if(target > index){
	    	$(".msgs").find(".msg[index='"+target+"']").before(msgElement);
	    	break;
	    }else if(target  < index && index < ind  ){
	    	$(".msgs").find(".msg[index='"+target+"']").after(msgElement);
	    	break;
	    }else if(index > ind && i == len){
	    	$(".msgs").find(".msg[index='"+ind+"']").after(msgElement);
	    	break;
	    }
	    target = ind;
	}
  }
  
  function event(){
 
	 $(".msgs").on("click",".msg_refresh",function(){
	      var msg = $(this).parents(".msg");
		  var rows = msg.find("tr");
		  var index = rows.index(msg.find("tr:visible").last());
		  if(index >= rows.length-1){
			  index = -1;
		  }
		  rows.hide();
		  rows.slice(index+1, index+11).removeClass("hide").show();
	  })
	  
	  
	  $('#system_msg').modal({"show":false})
	  $(".msgs").on("click",".systemmsg a.title",function(){
          $("#system_msg .modal-body .lead").html($(this).text());
          $('#system_msg').modal("show");
	  })
	  
  }